package com.sigmatrix.h5.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sigmatrix.h5.dao.DisDealerAcctRegAuditDao;
import com.sigmatrix.h5.dao.DisDealerDao;
import com.sigmatrix.h5.entity.DisDealerAcctRegAudit;
import com.sigmatrix.h5.enums.ApplyStatus;
import com.sigmatrix.h5.enums.ApplyType;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;
import com.sigmatrix.sc.jdbc.common.lang.Page;


/**
 * 
 *@ClassName: DisDealerAcctRegAuditDaoImpl
 *@Description: 经销商反向注册审核记录
 *@author MH
 *@date 2017年11月8日 上午9:18:27
 */
@Repository
public class DisDealerAcctRegAuditDaoImpl extends DaoImpl<DisDealerAcctRegAudit, Integer> implements DisDealerAcctRegAuditDao{
	@Autowired
	private DisDealerDao disDealerDao;
	
	@Override
	public Page<DisDealerAcctRegAudit> findAuditList(DisDealerAcctRegAudit po, int pageNo, int pageSize) {
		StringBuffer sql = new StringBuffer();
		List<Object> values = new ArrayList<Object>();
		sql.append("select b._id,b._basic_module_id,b._dealer_id,b._dealer_name,b._account,b._pwd,");
		sql.append("b._role_code,b._name,b._open_id,b._apply_time,b._apply_type,b._promotion_code,b._promotion_code_type,");
		sql.append("b._status,b._contact,b._additional_contact,b._reason,b._remarks,b._terminal_name from");
		sql.append("(select * from ").append(tableName()).append(" a");
		sql.append(" where 1=1 and exists (select 1 from ").append(disDealerDao.tableName());
		sql.append(" d where d._id = a._dealer_id and d._parent_id = ").append(po.getDealerId());
		sql.append(") and a._apply_type = ").append(ApplyType.DEALER.value);
		sql.append(" UNION ");
		sql.append("select * from ").append(tableName()).append(" a where 1=1 ");
		sql.append(" and a._apply_type = ").append(ApplyType.SHOP.value);
		sql.append("  and a._dealer_id = ").append(po.getDealerId());
		sql.append(") b").append(" where b._status = ").append(ApplyStatus.NO_AUDIT.value);
		if(CommonUtils.isNotEmpty(po.getName())){
			sql.append(" and b._name like N'%").append(po.getName()).append("%'");
		}
		if(CommonUtils.isNotEmpty(po.getPromotionCode())){
			sql.append(" and b._promotion_code = ?");
			values.add(po.getPromotionCode());
		}
		if(CommonUtils.isNotEmpty(po.getPromotionCodeType())){
			sql.append(" and b._promotion_code_type = ?");
			values.add(po.getPromotionCodeType());
		}
		return query(sql.toString(), null, pageNo, pageSize, new DefaultRowMapper(), values.toArray());
	}
	
	
}
